package cn.iocoder.yudao.module.game.service.otherGame;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.game.controller.admin.gameList.vo.GameListPageReqVO;
import cn.iocoder.yudao.module.game.controller.admin.gameList.vo.GameListSaveReqVO;
import cn.iocoder.yudao.module.game.dal.dataobject.gameList.GameListDO;
import cn.iocoder.yudao.module.game.dal.mysql.gameList.GameListMapper;
import cn.iocoder.yudao.module.game.utils.AESEncrypt;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import net.sf.json.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.game.enums.ErrorCodeConstants.LIST_NOT_EXISTS;

/**
 * 游戏列表 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class JDBServiceImpl implements JDBService {

    private String dc = "Q51S";
    // 测试
//    private String key = "86d4866c70b25dfc";
//    private String iv = "3d84f0695a914f88";
//    private String apiUrl = "https://api.jdb711.com/apiRequest.do";

    // 正式
    private String key = "c9bc03d6268f1379";
    private String iv = "96b70e3b0285e619";
    private String apiUrl = "https://api.jdb1688.net/apiRequest.do";

    private String parent = "q51sbrlag";

    @Override
    public JSONObject createPlayer(Map<String, Object> params) throws Exception {
        System.out.println("dc: " + dc);
        System.out.println("key: " + key);
        System.out.println("iv: " + iv);
        System.out.println("apiUrl: " + apiUrl);
        // prepare action 47 data
        JSONObject data = new JSONObject();
        data.put("action", 12);
        data.put("ts", System.currentTimeMillis());
        data.put("parent", parent);
        data.put("uid", params.get("userId"));
        data.put("name", params.get("userId"));
        System.out.println("data: " + data.toString());
        // encrypt
        String x = AESEncrypt.encrypt(data.toString(), key, iv);
        System.out.println("x: " + x);
        // build request
        List<NameValuePair> paramList = new ArrayList<>();
        paramList.add(new BasicNameValuePair("dc", dc));
        paramList.add(new BasicNameValuePair("x", x));
        UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList, "UTF-8");

        // get a client
        CloseableHttpClient client = HttpClientBuilder.create().build();

        HttpPost httpPost = new HttpPost(apiUrl);
        httpPost.setEntity(entity);
        // gzip
        // httpPost.setHeader("Accept-Encoding", "gzip");
        HttpEntity httpEntity = null;
        String res = "{}";
        try {
            // Resolve response
            HttpResponse response = client.execute(httpPost);
            httpEntity = response.getEntity();
            res = EntityUtils.toString(httpEntity);
            System.out.println("POST:  " + res);
        } catch (ClientProtocolException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            EntityUtils.consume(httpEntity);
        }

        return JSONObject.fromObject(res);
    }

    @Override
    public JSONObject login(Map<String, Object> params) throws Exception {
        System.out.println("dc: " + dc);
        System.out.println("key: " + key);
        System.out.println("iv: " + iv);
        System.out.println("apiUrl: " + apiUrl);
        // prepare action 47 data
        JSONObject data = new JSONObject();
        data.put("action", 21);
        data.put("ts", System.currentTimeMillis());
        data.put("parent", parent);
        data.put("uid", params.get("userId"));
        data.put("balance", params.get("balance"));
        data.put("gType", params.get("gType"));
        data.put("mType", params.get("mType"));
        data.put("lang", "en");
        data.put("windowMode", "2");
        System.out.println("data: " + data.toString());
        // encrypt
        String x = AESEncrypt.encrypt(data.toString(), key, iv);
//        x = "h2d_GFLFPfFOX8-sWjuRPgDMMbS6mLg9UkxPke8H7s8mMSiSVWBtyd6pgsL4WZCFh01Dt6MFvaggjA45-W2DzlgtvjOvc1bWHB9sO_1MErZfXnONq1M_OTW7Mt72HyduIXeaWCE_dEZEmtxGAMCmkNSVK9t26RLttt2-WwcwjxqsVCVcOIFygfqwPKJxlXV7";
        System.out.println("x: " + x);
        // build request
        List<NameValuePair> paramList = new ArrayList<>();
        paramList.add(new BasicNameValuePair("dc", dc));
        paramList.add(new BasicNameValuePair("x", x));
        UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList, "UTF-8");

        // get a client
        CloseableHttpClient client = HttpClientBuilder.create().build();

        HttpPost httpPost = new HttpPost(apiUrl);
        httpPost.setEntity(entity);
        // gzip
        // httpPost.setHeader("Accept-Encoding", "gzip");
        HttpEntity httpEntity = null;
        String res = "{}";
        try {
            // Resolve response
            HttpResponse response = client.execute(httpPost);
            httpEntity = response.getEntity();
            res = EntityUtils.toString(httpEntity);
            System.out.println("POST:  " + res);
        } catch (ClientProtocolException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            EntityUtils.consume(httpEntity);
        }

        return JSONObject.fromObject(res);
    }
}